package com.woniu.mapper;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.Dept;
import com.woniu.entity.Project;
import com.woniu.entity.PurApprove;
import com.woniu.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface PurApproveMapper extends BaseMapper<PurApprove> {

       //新增采购审批
        @Insert("insert into pur_approve(pur_id,dept_id, user_id, pur_apply_date, pur_approve_date, pur_approve_feedback, pur_approve_status) " +
                "values(#{proPurchase.purId},#{dept.deptId},#{user.userId},#{purApplyDate},#{purApproveDate},#{purApproveFeedback},#{purApproveStatus} )")
        void add(PurApprove purApprove);

        //删除采购审批
        @Delete("delete from pur_approve where pur_approve_id = #{purApproveId}  ")
        void delete(int purApproveId);

        //动态修改采购审批
        @UpdateProvider(type = SqlProvider.class,method = "update")
        void update(PurApprove purApprove);

        //查询所有审批
        @Select("select pap.*, pur.pur_name, user.user_name, dept.dept_name from pur_approve pap,user, dept, pro_purchase pur " +
                "where pap.pur_id = pur.pur_id  and pap.dept_id = dept.dept_id and pap.user_id = user.user_id ")
        @Results(id = "purApproveMap",value = {
                @Result(column = "pur_approve_id", property = "purApproveId",id = true),
                @Result(column = "pur_id",property = "proPurchase.purId"),
                @Result(column = "pur_name",property = "proPurchase.purName"),
                @Result(column = "dept_id",property = "dept.deptId"),
                @Result(column = "dept_name",property = "dept.deptName"),
                @Result(column = "user_id",property = "user.userId"),
                @Result(column = "user_name",property = "user.userName"),
                @Result(column = "pur_apply_date",property = "purApplyDate"),
                @Result(column = "pur_approve_date",property = "purApproveDate"),
                @Result(column = "pur_approve_feedback",property = "purApproveFeedback"),
                @Result(column = "pur_approve_status",property = "purApproveStatus")

        })
        List<PurApprove> getall();

        //根据采购审批id查询审批
        @Select("select pap.*, pur.pur_name, user.user_name, dept.dept_name from pur_approve pap,user, dept, pro_purchase pur " +
                "where pap.pur_id = pur.pur_id  and pap.dept_id = dept.dept_id and pap.user_id = user.user_id and pur_approve_id = #{purApproveId}")
        @ResultMap("purApproveMap")
        PurApprove getByPurApproveId(int purApproveId);

        //根据部门id查询审批
        @Select("select pap.*, pur.pur_name, user.user_name, dept.dept_name from pur_approve pap,user, dept, pro_purchase pur " +
                "where pap.pur_id = pur.pur_id  and pap.dept_id = dept.dept_id and pap.user_id = user.user_id and pap.dept_id = #{deptId}")
        @ResultMap("purApproveMap2")
        List<PurApprove> getByDeptId(int deptId);

        //根据用户id查询审批
        @Select("select pap.*, pur.pur_name, user.user_name, dept.dept_name from pur_approve pap,user, dept, pro_purchase pur " +
                "where pap.pur_id = pur.pur_id  and pap.dept_id = dept.dept_id and pap.user_id = user.user_id and pap.user_id = #{userId}")
        @ResultMap("purApproveMap2")
        List<PurApprove> getByUserId(int userId);

        //根据用户id查询未通过审批
        @Select("select pap.*, pur.pur_name, user.user_name, dept.dept_name from pur_approve pap,user, dept, pro_purchase pur " +
                "where pap.pur_id = pur.pur_id  and pap.dept_id = dept.dept_id and pap.user_id = user.user_id and pap.user_id = #{userId} and pur_approve_status = 'n' ")
        @ResultMap("purApproveMap2")
        List<PurApprove> getRefuseByPurUserId(int userId);

        //多条件分页查询采购审批
        @Select("select * from pur_approve ${ew.customSqlSegment} ")
        @ResultMap("purApproveMap2")
        Page<PurApprove> getPurApproveByPage(Page<PurApprove> page, @Param(Constants.WRAPPER) Wrapper<PurApprove> wrapper);


        //多条件查询采购审批
        @Select("select * from pur_approve ${ew.customSqlSegment} ")
        @Results(id = "purApproveMap2",value = {
                @Result(column = "pur_approve_id", property = "purApproveId",id = true),
                @Result(column = "pur_id",property = "proPurchase.purId"),
                @Result(column = "dept_id",property = "dept.deptId"),
                @Result(column = "user_id",property = "user.userId"),
                @Result(column = "pur_apply_date",property = "purApplyDate"),
                @Result(column = "pur_approve_date",property = "purApproveDate"),
                @Result(column = "pur_approve_feedback",property = "purApproveFeedback"),
                @Result(column = "pur_approve_status",property = "purApproveStatus")

        })
        List<PurApprove> getPurApproveByCondition (@Param(Constants.WRAPPER) Wrapper<PurApprove> wrapper );

        //同意审批
        @Update("update pur_approve set pur_approve_date = #{apdate},pur_approve_feedback = #{apfeedback},pur_approve_status = 'y'  where pur_approve_id = #{purApproveId} ")
        void pass(@Param("purApproveId") int purApproveId, @Param("apdate")String apdate,@Param("apfeedback") String apfeedback );

        //审批不通过
        @Update("update pur_approve set pur_approve_date = #{apdate},pur_approve_feedback = #{apfeedback},pur_approve_status = 'n'  where pur_approve_id = #{purApproveId} ")
        void refuse(@Param("purApproveId") int purApproveId, @Param("apdate")String apdate,@Param("apfeedback") String apfeedback );


        public static class SqlProvider {
                public static String update(PurApprove purApprove) {
                        String sql = new SQL(){
                                {
                                        //条件写法
                                        UPDATE("pur_approve");
                                        if(purApprove.getProPurchase() != null && purApprove.getProPurchase().getPurId() != 0 ) {
                                                SET("pur_id=#{proPurchase.purId}");
                                        }
                                        if(purApprove.getDept() != null && purApprove.getDept().getDeptId() != 0 ) {
                                            SET("dept_id == #{dept.deptId } ");
                                        }
                                        if(purApprove.getUser() != null && purApprove.getUser().getUserId() != 0 ) {
                                            SET("user_id = user.userId");
                                        }
                                        if(purApprove.getPurApplyDate() != null ) {
                                            SET("pur_apply_date =#{purApplyDate} ");
                                        }
                                        if(purApprove.getPurApproveDate() != null) {
                                            SET("pur_approve_date = #{purApproveDate}");
                                        }
                                        if(purApprove.getPurApproveFeedback() != null) {
                                            SET("pur_approve_feedback = #{purApproveFeedback}");
                                        }
                                        if(purApprove.getPurApproveStatus() != null) {
                                            SET("pur_approve_status = #{purApproveStatus}");
                                        }
                                        WHERE("pur_approve_id=#{purApproveId}");
                                }
                        }.toString();
                        return sql;
                }
        }



}
